In [2]:
# Load all importance packages
import geopandas
import numpy as np
import pandas as pd
from shapely.geometry import Point

import missingno as msn

import seaborn as sns
import matplotlib.pyplot as plt

% matplotlib inline
UsageError: Line magic function `%` not found.
In [3]:
country = geopandas.read_file("data/Neighbourhoods.geojson")
country.head()
Out[3]:
_id AREA_ID AREA_ATTR_ID PARENT_AREA_ID AREA_SHORT_CODE AREA_LONG_CODE AREA_NAME AREA_DESC X Y LONGITUDE LATITUDE OBJECTID Shape__Area Shape__Length geometry
0 3501 25886861 25926662 49885 94 94 Wychwood (94) Wychwood (94) None None -79.425515 43.676919 16491505 3.217960e+06 7515.779658 POLYGON ((-79.43592 43.68015, -79.43492 43.680...
1 3502 25886820 25926663 49885 100 100 Yonge-Eglinton (100) Yonge-Eglinton (100) None None -79.403590 43.704689 16491521 3.160334e+06 7872.021074 POLYGON ((-79.41096 43.70408, -79.40962 43.704...
2 3503 25886834 25926664 49885 97 97 Yonge-St.Clair (97) Yonge-St.Clair (97) None None -79.397871 43.687859 16491537 2.222464e+06 8130.411276 POLYGON ((-79.39119 43.68108, -79.39141 43.680...
3 3504 25886593 25926665 49885 27 27 York University Heights (27) York University Heights (27) None None -79.488883 43.765736 16491553 2.541821e+07 25632.335242 POLYGON ((-79.50529 43.75987, -79.50488 43.759...
4 3505 25886688 25926666 49885 31 31 Yorkdale-Glen Park (31) Yorkdale-Glen Park (31) None None -79.457108 43.714672 16491569 1.156669e+07 13953.408098 POLYGON ((-79.43969 43.70561, -79.44011 43.705...
In [4]:
country.plot(figsize=(30,20), color='#3B3C6E')
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x20155795a30>
In [7]:
florence = pd.read_csv('data/df_cleaned.csv')
florence.head()
florence['coordinates'] = florence[['X', 'Y']].values.tolist()
florence.head()
Out[7]:
Unnamed: 0 X Y event_unique_id Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence MCI Division Hood_ID Neighbourhood coordinates
0 0 -79.385193 43.659229 GO-20149004286 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over Theft Over D52 76 Bay Street Corridor (76) [-79.3851929, 43.65922929999999]
1 1 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm Assault D32 36 Newtonbrook West (36) [-79.42539980000001, 43.7775917]
2 2 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon Robbery D32 36 Newtonbrook West (36) [-79.42539980000001, 43.7775917]
3 3 -79.210373 43.801727 GO-20142412127 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E Break and Enter D42 132 Malvern (132) [-79.21037290000001, 43.801727299999996]
4 4 -79.254334 43.835884 GO-20142417548 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault Assault D42 130 Milliken (130) [-79.2543335, 43.8358841]
In [8]:
florence['coordinates'] = florence['coordinates'].apply(Point)
florence.head()
Out[8]:
Unnamed: 0 X Y event_unique_id Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence MCI Division Hood_ID Neighbourhood coordinates
0 0 -79.385193 43.659229 GO-20149004286 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over Theft Over D52 76 Bay Street Corridor (76) POINT (-79.38519290000001 43.65922929999999)
1 1 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm Assault D32 36 Newtonbrook West (36) POINT (-79.42539980000001 43.7775917)
2 2 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon Robbery D32 36 Newtonbrook West (36) POINT (-79.42539980000001 43.7775917)
3 3 -79.210373 43.801727 GO-20142412127 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E Break and Enter D42 132 Malvern (132) POINT (-79.21037290000001 43.8017273)
4 4 -79.254334 43.835884 GO-20142417548 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault Assault D42 130 Milliken (130) POINT (-79.2543335 43.8358841)
In [9]:
florence = geopandas.GeoDataFrame(florence, geometry='coordinates')
florence.head()
Out[9]:
Unnamed: 0 X Y event_unique_id Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence MCI Division Hood_ID Neighbourhood coordinates
0 0 -79.385193 43.659229 GO-20149004286 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over Theft Over D52 76 Bay Street Corridor (76) POINT (-79.38519 43.65923)
1 1 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm Assault D32 36 Newtonbrook West (36) POINT (-79.42540 43.77759)
2 2 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon Robbery D32 36 Newtonbrook West (36) POINT (-79.42540 43.77759)
3 3 -79.210373 43.801727 GO-20142412127 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E Break and Enter D42 132 Malvern (132) POINT (-79.21037 43.80173)
4 4 -79.254334 43.835884 GO-20142417548 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault Assault D42 130 Milliken (130) POINT (-79.25433 43.83588)
In [10]:
florence.plot(figsize=(20,10));
In [11]:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')
florence.plot(ax=base, color='Black', marker="*", markersize=100);
In [21]:
cam = pd.read_csv("data/Traffic Camera List.csv")
cam.head()
Out[21]:
_id Camera Number Latitude Longitude Main Road Cross Street Group Traffic Image North Reference Static Image East Reference Static Image South Reference Static Image West Reference Static Image
0 1 Camera8001 43.643079 -79.381407 YORK ST BREMNER BLVD Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc...
1 2 Camera8002 43.642220 -79.384068 BREMNER BLVD LOWER SIMCOE ST Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc...
2 3 Camera8003 43.649461 -79.371267 JARVIS ST FRONT ST E Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc...
3 4 Camera8004 43.645153 -79.382574 UNIVERSITY AVE FRONT ST W / YORK ST Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc...
4 5 Camera8005 43.644685 -79.384861 FRONT ST W SIMCOE ST / LOWER SIMCOE ST Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc...
In [22]:
cam['coordinates'] = cam[['Longitude', 'Latitude']].values.tolist()
cam.head()
Out[22]:
_id Camera Number Latitude Longitude Main Road Cross Street Group Traffic Image North Reference Static Image East Reference Static Image South Reference Static Image West Reference Static Image coordinates
0 1 Camera8001 43.643079 -79.381407 YORK ST BREMNER BLVD Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... [-79.381407, 43.643079]
1 2 Camera8002 43.642220 -79.384068 BREMNER BLVD LOWER SIMCOE ST Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... [-79.384068, 43.64222]
2 3 Camera8003 43.649461 -79.371267 JARVIS ST FRONT ST E Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... [-79.371267, 43.649461]
3 4 Camera8004 43.645153 -79.382574 UNIVERSITY AVE FRONT ST W / YORK ST Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... [-79.382574, 43.645153]
4 5 Camera8005 43.644685 -79.384861 FRONT ST W SIMCOE ST / LOWER SIMCOE ST Arterial http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... http://opendata.toronto.ca/transportation/tmc... [-79.384861, 43.644685]
In [23]:
cam = cam.drop(['Group', 'Traffic Image', 'North Reference Static Image','East Reference Static Image','South Reference Static Image','West Reference Static Image'], axis=1)
cam
Out[23]:
_id Camera Number Latitude Longitude Main Road Cross Street coordinates
0 1 Camera8001 43.643079 -79.381407 YORK ST BREMNER BLVD [-79.381407, 43.643079]
1 2 Camera8002 43.642220 -79.384068 BREMNER BLVD LOWER SIMCOE ST [-79.384068, 43.64222]
2 3 Camera8003 43.649461 -79.371267 JARVIS ST FRONT ST E [-79.371267, 43.649461]
3 4 Camera8004 43.645153 -79.382574 UNIVERSITY AVE FRONT ST W / YORK ST [-79.382574, 43.645153]
4 5 Camera8005 43.644685 -79.384861 FRONT ST W SIMCOE ST / LOWER SIMCOE ST [-79.384861, 43.644685]
... ... ... ... ... ... ... ...
275 276 Camera9403 43.711222 -79.442755 W R ALLEN RD GLENGROVE AVE [-79.442755, 43.711222]
276 277 Camera9404 43.716458 -79.444790 W R ALLEN RD LAWRENCE AVE W [-79.44479, 43.716458]
277 278 Camera9405 43.728143 -79.448634 W R ALLEN RD 401 C E ALLEN RD RAMP [-79.448634, 43.728143]
278 279 Camera9406 43.739609 -79.452570 W R ALLEN RD TRANSIT RD [-79.45257, 43.739609]
279 280 Camera9407 43.749974 -79.463592 W R ALLEN RD SHEPPARD AVE W [-79.463592, 43.749974]

280 rows × 7 columns

In [24]:
cam['coordinates'] = cam['coordinates'].apply(Point)
cam.head()
cam = geopandas.GeoDataFrame(cam, geometry='coordinates')
cam.head()
Out[24]:
_id Camera Number Latitude Longitude Main Road Cross Street coordinates
0 1 Camera8001 43.643079 -79.381407 YORK ST BREMNER BLVD POINT (-79.38141 43.64308)
1 2 Camera8002 43.642220 -79.384068 BREMNER BLVD LOWER SIMCOE ST POINT (-79.38407 43.64222)
2 3 Camera8003 43.649461 -79.371267 JARVIS ST FRONT ST E POINT (-79.37127 43.64946)
3 4 Camera8004 43.645153 -79.382574 UNIVERSITY AVE FRONT ST W / YORK ST POINT (-79.38257 43.64515)
4 5 Camera8005 43.644685 -79.384861 FRONT ST W SIMCOE ST / LOWER SIMCOE ST POINT (-79.38486 43.64469)
In [25]:
cam.plot(figsize=(20,10));
In [27]:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')


florence.plot(ax=base, color='Black', marker="*", markersize=100);

# camera around toronto
cam.plot(ax=base, color='Red', marker="*", markersize=500);
In [36]:
be = ['Break and Enter']
florence2 = florence[florence.MCI.isin(be)]
florence2.head()
Out[36]:
Unnamed: 0 X Y event_unique_id Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence MCI Division Hood_ID Neighbourhood coordinates
3 3 -79.210373 43.801727 GO-20142412127 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E Break and Enter D42 132 Malvern (132) POINT (-79.21037 43.80173)
5 5 -79.273819 43.709972 GO-20142524077 00:01 2014-07-18 2014-07-18 17:36 House 2120 200 B&E Break and Enter D41 120 Clairlea-Birchmount (120) POINT (-79.27382 43.70997)
8 8 -79.237824 43.791515 GO-20142531700 15:00 2014-07-19 2014-07-19 22:11 House 2120 220 B&E W'Intent Break and Enter D42 132 Malvern (132) POINT (-79.23782 43.79152)
9 9 -79.435112 43.717243 GO-20142536209 01:30 2014-07-20 2014-07-20 16:33 Other 2120 220 B&E W'Intent Break and Enter D13 32 Englemount-Lawrence (32) POINT (-79.43511 43.71724)
13 13 -79.296059 43.689453 GO-20142102880 03:52 2014-05-18 2014-05-18 12:45 Commercial 2120 220 B&E W'Intent Break and Enter D54 62 East End-Danforth (62) POINT (-79.29606 43.68945)
In [37]:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')

florence2.plot(ax=base, color='Black', marker="*", markersize=100);

# camera around toronto
cam.plot(ax=base, color='Red', marker="*", markersize=500);
In [38]:
be = ['Auto Theft']
florence3 = florence[florence.MCI.isin(be)]
florence3.head()
Out[38]:
Unnamed: 0 X Y event_unique_id Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence MCI Division Hood_ID Neighbourhood coordinates
149000 149000 -79.457169 43.681034 GO-20141360638 02:00 2014-01-17 2014-01-17 12:36 House 2135 210 Theft Of Motor Vehicle Auto Theft D13 92 Corso Italia-Davenport (92) POINT (-79.45717 43.68103)
149001 149001 -79.361267 43.758724 GO-20181866775 18:00 2018-10-07 2018-10-09 18:13 Outside 2135 210 Theft Of Motor Vehicle Auto Theft D33 40 St.Andrew-Windfields (40) POINT (-79.36127 43.75872)
149002 149002 -79.379715 43.719639 GO-20141359460 22:00 2014-01-16 2014-01-18 12:08 House 2135 210 Theft Of Motor Vehicle Auto Theft D53 41 Bridle Path-Sunnybrook-York Mills (41) POINT (-79.37972 43.71964)
149003 149003 -79.295113 43.700878 GO-20141371716 19:00 2014-01-18 2014-01-19 08:53 House 2135 210 Theft Of Motor Vehicle Auto Theft D54 61 Taylor-Massey (61) POINT (-79.29511 43.70088)
149004 149004 -79.493752 43.671673 GO-20181892206 22:00 2018-10-12 2018-10-13 13:05 Outside 2135 210 Theft Of Motor Vehicle Auto Theft D12 111 Rockcliffe-Smythe (111) POINT (-79.49375 43.67167)
In [39]:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')

florence3.plot(ax=base, color='Black', marker="*", markersize=100);

# camera around toronto
cam.plot(ax=base, color='Red', marker="*", markersize=500);
In [46]:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')


florence.plot(ax=base, color='Black', marker="*", markersize=100);

# camera around toronto
cam.plot(ax=base, color='Red', marker="*", markersize=500);
florence.plot(ax=base, column='MCI', marker=">", markersize=100, cmap='tab20c', label="Wind speed(mph)")
_ = ax.axis('off')
plt.legend()
ax.set_title("Toronto  Map", fontsize=25)
plt.savefig('Toronto.png',bbox_inches='tight');
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: